<?php
include 'header.php';
@$incomeType = $_REQUEST['incomeType'];
@$year = $_REQUEST['year'];
@$month = $_REQUEST['month'];
@$day = $_REQUEST['day'];

$allMoney = 0;
$yearSql = "date_format(payDate,'%Y')";
$monthSql = "date_format(payDate,'%m')";
$daySql = "date_format(payDate,'%d')";

$where = "where pay='是'";
$orderBy = " order by $yearSql";

@$searchYear = $_REQUEST['searchYear'];
@$searchMonth = $_REQUEST['searchMonth'];
@$searchDay = $_REQUEST['searchDay'];
@$begin = $_REQUEST['begin'];
@$end = $_REQUEST['end'];

$sql1 = "select distinct $yearSql as year from `order` $where";
$where1 = null;
$array1 = null;
if(!empty($searchYear)){															//年搜索
	$where1 = " and $yearSql=:searchYear";
	$array1['searchYear'] = $searchYear;
	$incomeType = "year";
}
@$allYear = dbSelectAll($sql1);			//查数据库所有年份
$sql1 .= $where1.$orderBy;
@$allYears = dbSelectAll($sql1,$array1);			//查所有年份

$sql2 = "select distinct $monthSql as month,$yearSql as year from `order`";
$where2 = " where $yearSql=:year";
$array2 = array('year'=>$year);
if(!empty($searchMonth)){
		$array2 = null;
		$incomeType = "month";
		$sql2 = "select distinct date_format(payDate,'%Y-%m') as ym from `order`";
	if(empty($searchYear)){															//月搜索
		$where2 = " where date_format(payDate,'%m')=:m";
		$array2['m'] = $searchMonth;	
	}else{																			//年月搜索
		$where2 = " where date_format(payDate,'%Y-%m')=:ym";
		$array2['ym'] = $searchYear.'-'.$searchMonth;
	}
}
$sql2 .= $where2;
@$months = dbSelectAll($sql2,$array2);				//用年查月

$sql3 = "select distinct $daySql as day from `order`";
$where3 = " where date_format(payDate,'%Y-%m')=:ym";
$array3 = array('ym'=>$year.'-'.$month);
if(!empty($searchDay)){
		$array3 = null;
		$incomeType = "day";
		$sql3 = "select distinct date_format(payDate,'%Y-%m-%d') as ymd from `order`";
	if(empty($searchMonth) && empty($searchYear)){									//日搜索
		$where3 = " where date_format(payDate,'%d')=:d";
		$array3['d'] = $searchDay;
	}else if(!empty($searchMonth) && !empty($searchYear)){							//年月日搜索
		$where3 = " where date_format(payDate,'%Y-%m-%d')=:ymd";
		$array3['ymd'] = $searchYear.'-'.$searchMonth.'-'.$searchDay;
	}else if(!empty($searchMonth)){													//月日搜索
		$where3 = " where date_format(payDate,'%m-%d')=:md";
		$array3['md'] = $searchMonth.'-'.$searchDay;
	}else if(!empty($searchYear)){													//年日搜索
		$where3 = " where date_format(payDate,'%Y-%d')=:yd";
		$array3['yd'] = $searchYear.'-'.$searchDay;
	}
}
if(!empty($begin) && !empty($end)){														//范围收索
	$array3 = null;
	$incomeType = "day";
	$sql3 = "select distinct date_format(payDate,'%Y-%m-%d') as ymd from `order`";
	$where3 = " where date_format(payDate,'%Y-%m-%d') between :begin and :end";
	$array3['begin'] = $begin;
	$array3['end'] = $end;
}
$sql3 .= $where3;
@$days = dbSelectAll($sql3,$array3);				//用年月查日

@$allMonths = dbSelectAll("select distinct $monthSql as month from `order` $where order by $monthSql");			//查所有月份
@$allDays = dbSelectAll("select distinct $daySql as day from `order` $where order by $daySql");			//查所有天

?>
		<div class='container'>
   			<div class='row'>
   				<?php if(empty($day)){?>
   				<div class='col-xs-2'>
   					<a href='index.php' class='btn btn-default btn-block'>返回首页</a>
   					<?php if($incomeType =="all" || empty($incomeType)){?>
   						<a href='?incomeType=year' class='btn btn-default btn-block'>按年收入</a>
   					<?php }else if($incomeType == "year"){?>
   						<a href='?incomeType=all' class='btn btn-default btn-block'>总收入</a>
   					<?php }else if($incomeType == "month"){?>
   						<a href='?incomeType=all' class='btn btn-default btn-block'>总收入</a>
   						<a href="?incomeType=year" class='btn btn-primary btn-block'>返回上一级</a>
   					<?php }else if($incomeType == "day"){?>
   						<a href='?incomeType=all' class='btn btn-default btn-block'>总收入</a>
   						<a href="?incomeType=month&&year=<?php echo $year;?>" class='btn btn-primary btn-block'>返回上一级</a>
   					<?php }?>
   				</div>
   				<div class='col-xs-8'>
   					<div class='panel panel-primary'>  
   						<div class='panel-heading'>
   							<div class='row'>
   								<div class='col-xs-3'>
   									<?php if($incomeType == "year"){?>
		   								<div class='panel-title incomeTitle'>按年收入</div>
		   							<?php }else if($incomeType == "month"){?>
		   								<div class='panel-title incomeTitle'><?php echo (empty($searchMonth)?$year:'所有').'年';?>>>月收入</div>
		   							<?php }else if($incomeType == "day"){?>
		   								<div class='panel-title incomeTitle'><?php echo empty($searchDay)?$year.'年'.$month.'月':'所有';?>>>天收入</div>
		   							<?php }else if($incomeType =="all" || empty($incomeType)){?>
		   								<div class='panel-title incomeTitle'>总收入</div>
		   							<?php }?>
   								</div>
   								<div class='col-xs-1 col-xs-offset-3' style='color:#000;'>
   									<button class='btn btn-default' style='height:32px;' data-toggle='modal' data-target='#search'>按时间段查询</button>
   									<div class='modal' id='search'>
   										<div class='modal-dialog' style='width:400px;'>
   											<div class='modal-content'>
   												<div class='modal-header'>
   													<div class='modal-title'>输入时间段</div>
   												</div>
   												<form action='#' class='form-horizontal'>
	   												<div class='modal-body'>
														<div class='form-group'>
															<label class='col-xs-3 control-label' for='begin'>开始时间</label>
															<div class='col-xs-9'>
																<input class='form-control form_date' id='begin' name='begin' />
															</div>
														</div>	   														
														<div class='form-group'>
															<label class='col-xs-3 control-label' for='end'>结束时间</label>
															<div class='col-xs-9'>
																<input class='form-control form_date' id='end' name='end' />
															</div>
														</div>	   														
	   												</div>
	   												<div class='modal-footer'>
	   													<button class='btn btn-primary' type='submit'>搜索</button>
	   													<a href='#' class='btn btn-default' data-dismiss='modal'>返回</a>
	   												</div>
   												</form>
   											</div>
   										</div>
   									</div>
   								</div>
   								<div class='col-xs-4 pull-right'>
   									<form action='#'>
		   								<div class='input-group' style='margin-left:-20px;'>
		   									<div class='input-group-btn'>
		   										<select id='searchYear' class='btn btn-default' name='searchYear'>
		   											<option value=''>年</option>
		   											<?php foreach ($allYear as $data){?>
		   												<option value='<?php echo $data['year'];?>' <?php echo $data['year']==$searchYear?"selected='selected'":'';?>><?php echo $data['year'];?></option>
		   											<?php }?>
		   										</select>
		   										<select id='searchMonth' class='btn btn-default' name='searchMonth'>
		   											<option value=''>月</option>
		   											<?php foreach ($allMonths as $allMonth){?>
		   												<option value='<?php echo $allMonth['month'];?>' <?php echo $allMonth['month']==$searchMonth?"selected='selected'":'';?>><?php echo $allMonth['month'];?></option>
		   											<?php }?>
		   										</select>
		   										<select id='searchDay' class='btn btn-default' name='searchDay'>
		   											<option value=''>日</option>
		   											<?php foreach ($allDays as $allDay){?>
		   												<option value='<?php echo $allDay['day'];?>' <?php echo $allDay['day']==$searchDay?"selected='selected'":'';?>><?php echo $allDay['day'];?></option>
		   											<?php }?>
		   										</select>
		   										<button class='btn btn-default' id='searchBtn' type='submit' style='height:32px;'>
		   											<span class='glyphicon glyphicon-search'></span>
		   										</button>
		   									</div>
		   								</div>
		   							</form>
   								</div>
   							</div>
   						</div>
   						<?php if($incomeType == "year"){?>
	   						<table class='typeTable table table-bordered'>
	   							<tr>
	   								<th>年份</th>
	   								<th>收入(单位¥)</th>
	   							</tr>
	   						<?php 
	   							foreach ($allYears as $year){
	   							@$aa = dbSelect("select sum(income) as income,$yearSql as year from `order` where $yearSql=:year",
	   												array('year'=>$year['year']));
	   							?>
	   							<tr>
	   								<td>
	   									<a href='?year=<?php echo $year['year'];?>&&incomeType=month'><?php echo $year['year'];?>年</a>
	   								</td>
	   								
	   								<td class='money'><?php echo $aa['income'];?></td>
	   							</tr>
	   						<?php }?>
	   						</table>
	   					<?php }else if($incomeType == "month"){?>
	   						<table class='typeTable table table-bordered'>
	   							<tr>
	   								<th>月份</th>
	   								<th>收入(单位¥)</th>
	   							</tr>
	   					<?php 
	   						foreach ($months as $month){
	   							if(empty($searchMonth)){
	   								$arr = array('ym'=>$year.'-'.$month['month']);
	   							}else{
	   								$arr = array('ym'=>$month['ym']);
	   							}
	   							@$monthIncomes = dbSelect("select sum(income) as income,$monthSql as month,$yearSql as year from `order` where date_format(payDate,'%Y-%m')=:ym",
	   									$arr);
	   					?>
	   							<tr>
	   								<td>
	   									<a href='?month=<?php echo $monthIncomes['month'];?>&&year=<?php echo (empty($searchMonth)?$year:$monthIncomes['year']);?>&&incomeType=day'><?php echo (empty($searchMonth)?$year:$monthIncomes['year']).'年'.$monthIncomes['month'];?>月</a>
	   								</td>
	   								<td class='money'><?php echo $monthIncomes['income'];?></td>
	   							</tr>
	   							
	   						<?php }?>
	   						</table>
	   					<?php }else if($incomeType == "day"){?>
	   						<table class='typeTable table table-bordered'>
	   							<tr>
	   								<?php if(!empty($begin) && !empty($end)){?>
	   									<th>日期范围</th>
	   								<?php }?>
	   								<th>DAY</th>
	   								<th>收入(单位¥)</th>
	   							</tr>
	   							<?php 
	   								foreach ($days as $day){
		   								if(!empty($searchDay || !empty($begin) && !empty($end))){
			   								$arr = array('ymd'=>$day['ymd']);
			   							}else if(empty($searchDay)){
			   								$arr = array('ymd'=>$year.'-'.$month.'-'.$day['day']);
			   							}
	   									@$dayIncomes = dbSelect("select sum(income) as income,$yearSql as year,$monthSql as month,$daySql as day from `order` where date_format(payDate,'%Y-%m-%d')=:ymd",$arr);
	   								?>
	   									<tr>
	   										<?php if(!empty($begin) && !empty($end)){?>
			   									<td><?php echo $begin.'到'.$end;?></td>
			   								<?php }?>
			   								<td>
			   									<a href='?year=<?php echo empty($searchDay)?$year:$dayIncomes['year'];?>&&month=<?php echo empty($searchDay)?$month:$dayIncomes['month'];?>&&day=<?php echo $dayIncomes['day'];?>'>
		   											<?php if(!empty($searchDay || !empty($begin) && !empty($end))){
		   												echo $dayIncomes['year'].'年'.$dayIncomes['month'].'月'.$dayIncomes['day'];?>日
		   											<?php }else{
		   												echo $year.'年'.$month.'月'.$dayIncomes['day'];?>日
		   											<?php }?>
			   									</a>
			   								</td>
			   								<td class='money'><?php echo $dayIncomes['income'];?></td>
			   							</tr>
	   								<?php }
	   							?>
	   						</table>
	   					<?php }else if($incomeType =="all" || empty($incomeType)){
	   						@$allIncomes = dbSelectAll("select income from `order`");
	   						foreach ($allIncomes as $allIncome){
	   							$allMoney += $allIncome['income'];
	   						}
	   					?>
	   						<table class='typeTable table table-bordered'>
	   							<tr>
	   								<th>本店自开张以来总盈利<span class='money'><?php echo $allMoney;?>RMB</span></th>
	   							</tr>
	   						</table>
	   					<?php }?>
   						<div class='panel-body'>
   							
   						</div>
   					</div>
   				</div>
   				<?php }else{?>
   					<div class='col-xs-2'>
   						<a href='index.php' class='btn btn-default btn-block'>返回首页</a>
   						<a href='?incomeType=all' class='btn btn-default btn-block'>总收入</a>
   						<a href="orderList.php?orderType=all" class='btn btn-default btn-block'>全部订单</a>
   						<a href="?incomeType=day&&year=<?php echo $year;?>&&month=<?php echo $month;?>" class='btn btn-primary btn-block'>返回上一级</a>
					</div>
					<div class='col-xs-10'>
						<div class='panel panel-primary'>
   							<div class='panel-heading'>
   								<div class='panel-title'>
   									<?php echo $year;?>年<?php echo $month;?>月<?php echo $day;?>日所有订单记录
   								</div>
   							</div>
   							<table class='table table-bordered text-center'>
   								<tr>
	   								<th class='text-center'>名称</th>
	   								<th class='text-center'>应付金额</th>
	   								<th class='text-center'>实付金额</th>
	   								<th class='text-center'>下单时间</th>
	   								<th class='text-center'>结账时间</th>
	   								<th class='text-center'>商品明细</th>
	   							</tr>
   								<?php 
	   								@$pageNum = $_REQUEST['pageNum'];
	   								$pageNum = pageNum($pageNum);
	   								$pageSize = 10;
	   								$where .= " and payDate like '$year-$month-$day%'";
	   								
	   								$sql = "select id,name,goodsPrice,payPrice,startDate,payDate,pay from `order` $where";
	   								@$allOrders = dbSelectPage($sql,$pageNum,$pageSize);
	   								@$count = dbSelectAll($sql);
	   								$num = ceil(count($count)/$pageSize);
	   								
	   								foreach($allOrders as $data){
	   									@$goods_orders = dbSelectAll('select imgUrl,goods.name as gName,type.name as tName,sellPrice,`count` from goods_order
																	join goods on goods.id=goods_order.goodsId
	   																join `type` on goods.typeId=type.id where goods_order.orderId=:id',array('id'=>$data['id']));
   								?>
   								<tr id='orderTtr<?php echo $data['id'];?>'>
   									<?php if($data['pay'] == "是"){?>
	   									<td><?php echo $data['name'];?></td>
   									<?php }else{?>
		   							<td>
   										<a href="index.php?noPayOrderId=<?php echo $data['id'];?>">
   											<?php echo $data['name'];?>
   										</a>
   									</td>
   									<?php }?>
	   								<td><?php echo $data['goodsPrice'];?></td>
	   								<td><?php echo $data['payPrice'];?></td>
	   								<td><?php echo $data['startDate'];?></td>
	   								<td><?php echo $data['payDate'];?></td>
	   								<td>
	   									<a href='#goods_order<?php echo $data['id'];?>' data-toggle='modal'>商品明细</a>
	   									<div class='modal' id='goods_order<?php echo $data['id'];?>'>
	   										<div class='modal-dialog'>
	   											<div class='modal-content'>
		   											<div class='modal-header'>
		   												<h4 class='modal-title'>
		   													[<?php echo $data['name']?>]所有商品
		   												</h4>
		   											</div>
		   											<div class='modal-body'>
			   											<table class='table table-bordered text-center'>
			   												<tr>
			   													<th class='text-center'>图片</th>
			   													<th class='text-center'>名称</th>
			   													<th class='text-center'>单价</th>
			   													<th class='text-center'>数量</th>
			   													<th class='text-center'>类型</th>
			   												</tr>
			   												<?php foreach($goods_orders as $goods_order){?>
			   													<tr>
			   														<td><img src='img/<?php echo $goods_order['imgUrl'];?>' class='goodsImg' /></td>
			   														<td><?php echo $goods_order['gName'];?></td>
			   														<td><?php echo $goods_order['sellPrice'];?></td>
			   														<td><?php echo $goods_order['count'];?></td>
			   														<td><?php echo $goods_order['tName'];?></td>
			   													</tr>
			   												<?php }?>
			   											</table>
			   										</div>
		   											<div class='modal-footer text-right'>
		   												<a href='#' class='btn btn-default' data-dismiss='modal'>返回</a>
		   											</div>
	   											</div>
	   										</div>
	   									</div>
	   									<?php if($data['pay'] == "是"){
											@$clientele = dbSelect('select clientele.name as cName,phone,location from clientele join `order` on clientele.id=`order`.clienteleId where `order`.id=:id',
																	array('id'=>$data['id']))			   										
	   									?>
	   										<a href='#clientele_order<?php echo $data['id'];?>' data-toggle='modal'>客户明细</a>
	   										<div class='modal' id='clientele_order<?php echo $data['id'];?>'>
		   										<div class='modal-dialog'>
		   											<div class='modal-content'>
			   											<div class='modal-header'>
			   												<h4 class='modal-title'>
			   													[<?php echo $data['name']?>]所属客户
			   												</h4>
			   											</div>
			   											<div class='modal-body'>
				   											<table class='table table-bordered text-center'>
				   												<tr>
				   													<th class='text-center'>姓名</th>
				   													<th class='text-center'>电话</th>
				   													<th class='text-center'>地址</th>
				   												</tr>
			   													<tr>
			   														<td><?php echo $clientele['cName'];?></td>
			   														<td><?php echo $clientele['phone'];?></td>
			   														<td><?php echo $clientele['location'];?></td>
			   													</tr>
				   											</table>
				   										</div>
			   											<div class='modal-footer text-right'>
			   												<a href='#' class='btn btn-default' data-dismiss='modal'>返回</a>
			   											</div>
		   											</div>
		   										</div>
		   									</div>
	   									<?php }?>
	   								</td>
	   							</tr>
	   							<?php }?>
   							</table>
   							<div class='panel-body'>
	   							<div class='pageDiv text-center'>
	   								<a href='?pageNum=<?php echo '1';?>' class='btn btn-default'>首页</a>
									<?php if($pageNum > 1){?>
		   								<a href='?pageNum=<?php echo $pageNum -1;?>' class='btn btn-default'>上一页</a>
									<?php }else{?>
										上一页
									<?php }?>
									<?php if($pageNum < $num){?>
		   								<a href='?pageNum=<?php echo $pageNum +1;?>' class='btn btn-default'>下一页</a>
									<?php }else{?>
										下一页
									<?php }?>
	   								<a href='?pageNum=<?php echo $num;?>' class='btn btn-default'>尾页</a>
	   							</div>
	   						</div>
   						</div>
					</div>
   				<?php }?>
   			</div>
   		</div>
   		<script>
	   		$('.form_date').datetimepicker({
				autoclose:true,
				language:'zh-CN',
				format:'yyyy-mm-dd'
			});
   		</script>
   	</body>
</html>